﻿using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Matrix.Common.DBUtility;
using Matrix.MaKaYunShang.Model;

namespace Matrix.MaKaYunShang.DAL
{
	public partial class CouponCommodityLibDAL : BaseDAL<CouponCommodityLibDAL>
	{
		#region 默认方法
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(CouponCommodityLib model)
		{
			var strSql=new StringBuilder();
			strSql.Append("insert into CouponCommodityLib(");			
            strSql.Append("CouponId,CommodityLibId,IsIgnorPrice,IsDelete");
			strSql.Append(") values (");
            strSql.Append("@CouponId,@CommodityLibId,@IsIgnorPrice,@IsDelete");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@CouponId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CommodityLibId", SqlDbType.Int,4) ,            
                        new SqlParameter("@IsIgnorPrice", SqlDbType.Bit,1) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };
			            
            parameters[0].Value = model.CouponId;                        
            parameters[1].Value = model.CommodityLibId;                        
            parameters[2].Value = model.IsIgnorPrice;                        
            parameters[3].Value = model.IsDelete;            			var obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			return obj == null ? 0 : Convert.ToInt32(obj);
		}
				
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(CouponCommodityLib model)
		{
			var strSql=new StringBuilder();
			strSql.Append("update CouponCommodityLib set ");
			                                                
            strSql.Append(" CouponId = @CouponId , ");                                    
            strSql.Append(" CommodityLibId = @CommodityLibId , ");                                    
            strSql.Append(" IsIgnorPrice = @IsIgnorPrice , ");                                    
            strSql.Append(" IsDelete = @IsDelete  ");            			
			strSql.Append(" where Id=@Id ");
						
			SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@CouponId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CommodityLibId", SqlDbType.Int,4) ,            
                        new SqlParameter("@IsIgnorPrice", SqlDbType.Bit,1) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };
						            
            parameters[0].Value = model.Id;                        
            parameters[1].Value = model.CouponId;                        
            parameters[2].Value = model.CommodityLibId;                        
            parameters[3].Value = model.IsIgnorPrice;                        
            parameters[4].Value = model.IsDelete;                        return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
		}
				
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			var strSql=new StringBuilder();
			strSql.Append("update CouponCommodityLib set IsDelete=1 ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			var strSql=new StringBuilder();
			strSql.Append("update CouponCommodityLib set IsDelete=1 ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
		}
						
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DataSet GetModel(int Id)
		{
			var strSql=new StringBuilder();
			strSql.Append("select Id, CouponId, CommodityLibId, IsIgnorPrice, IsDelete  ");			
			strSql.Append("  from CouponCommodityLib ");
			strSql.Append(" where Id=@Id  and IsDelete=0");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;
			
			var ds = DbHelperSQL.Query(strSql.ToString(),parameters);			
			return ds.Tables[0].Rows.Count>0 ? ds : null;
		}
				
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			var strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM CouponCommodityLib where IsDelete=0 ");
			if(!string.IsNullOrEmpty(strWhere))
			{
				strSql.Append(" and "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}		
		#endregion
		
		#region 扩展方法


        public bool DeleteByCommodityLibId(int couponId)
        {
            var strSql = new StringBuilder();
            strSql.Append("update CouponCommodityLib set IsDelete=1 ");
            strSql.Append(" where CouponId=@CouponId");
            SqlParameter[] parameters = {
					new SqlParameter("@CouponId", SqlDbType.Int,4)
			};
            parameters[0].Value = couponId;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 通过码云卡券集合获取码云卡券限制使用商品列表
        /// </summary>
        /// <param name="couponIds"></param>
        /// <returns></returns>
        public List<CouponCommodityLib> GetListByCouponIds(string couponIds)
        {
            var strSql = new StringBuilder();
            strSql.Append("select Id, CouponId, CommodityLibId, IsIgnorPrice, IsDelete  ");
            strSql.Append("  from CouponCommodityLib ");
            strSql.Append(" where CouponId IN (" + couponIds + ")  and IsDelete=0");

            var lsRet = DbHelperSQL.ExecuteSqlList<CouponCommodityLib>(strSql.ToString());
            return lsRet == null || lsRet.Count == 0 ? null : lsRet.ToList();
        }
		#endregion
	}
}
